package com.xinhe.web.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
    public static final String DATE_TIME_MILLI_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_PATTERN = "yyyy-MM-dd";
    public static final String TIME_PATTERN = "HH:mm:ss";
    public static final String DATE_TIME_SLASH_PATTERN = "yyyy/MM/dd HH:mm:ss";
    public static final String DATE_SLASH_PATTERN = "yyyy/MM/dd";
    public static final String DATE_TIME_MILLI_CODE_PATTERN = "yyyyMMddHHmmssSSS";
    public static final String DATE_TIME_CODE_PATTERN = "yyyyMMddHHmmss";
    public static final String DATE_CODE_PATTERN = "yyyyMMdd";
    public static final String TIME_CODE_PATTERN = "HHmmss";
    public static final String CHINA_TIME_ZONE = "GMT+8";
    public static final Integer DAY_RATE = Integer.valueOf(86400000);

    public static Date getMinDate() {
        Date date = new Date();
        date.setTime(0L);
        return date;
    }

    public static Long getCurrentUnix() {
        return Long.valueOf(System.currentTimeMillis() / 1000L);
    }

    public static String dateToStringDistanceCurrent(Date dateTime) {
        if (dateTime != null) {

            long second = ((new Date()).getTime() - dateTime.getTime()) / 1000L;

            long minute = second / 60L;

            long hour = minute / 60L;

            long day = hour / 24L;

            long month = day / 30L;

            long year = month / 12L;

            if (second < 60L)
                return second + "秒前";
            if (minute < 60L)
                return minute + "分钟前";
            if (hour < 24L)
                return hour + "小时前";
            if (day < 30L)
                return day + "天前";
            if (month < 12L) {
                return month + "个月前";
            }
            return year + "年前";
        }

        return null;
    }

    public static Date addMinute(Date start, Integer minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);
        calendar.add(12, minute.intValue());
        return calendar.getTime();
    }

    public static Date getDayMaxTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(10, 23);
        calendar.set(12, 59);
        calendar.set(13, 59);
        calendar.set(14, 999);
        return calendar.getTime();
    }

    public static Date getDayMinTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(10, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        return calendar.getTime();
    }

    public static Date getMonthMinTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        if (null != date) {
            calendar.setTime(date);
        }
        calendar.set(5, 0);
        calendar.set(10, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        return calendar.getTime();
    }
    public static Date getMonthMaxTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        if (null != date) {
            calendar.setTime(date);
        }
        calendar.set(2, calendar.get(2) + 1);
        calendar.add(5, -1);
        calendar.set(10, 23);
        calendar.set(12, 59);
        calendar.set(13, 59);
        calendar.set(14, 999);
        return calendar.getTime();
    }

    public static String formatDate(Date date, String pattern) {
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }

    public static Date parseDate(String date, String pattern) {
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        try {
            return dateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();

            return null;
        }
    }

    public static int betweenDay(Date start, Date end) {
        Long temp = Long.valueOf((start.getTime() - end.getTime()) / DAY_RATE.intValue());
        return temp.intValue();
    }
}

